package br.com.dao;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Query;
import org.hibernate.Session;
import br.com.model.Pessoa;
import br.com.util.BancoUtil;

/**
 * @author ramonsv89
 *
 */

public class PessoaDAO {

	public boolean adicionar(Pessoa pessoa){
		try{
			Session session = BancoUtil.getFactory().openSession();
			session.beginTransaction();
			session.save(pessoa);
			session.getTransaction().commit();
			session.close();
			return true;
		}catch (Exception e){
			e.printStackTrace();
		}	
		return false;
	}
	
	public void editar(Pessoa pessoa) throws SQLException{
		Session session = BancoUtil.getFactory().openSession();
		session.beginTransaction();
		session.saveOrUpdate(pessoa);
		session.getTransaction().commit();
		session.close();
	}
	
	public void excluir(Pessoa pessoa) throws SQLException{
		Session session = BancoUtil.getFactory().openSession();
		session.beginTransaction();
		session.delete(pessoa);session.getTransaction().commit();
		session.close();
	}
	
	public List<Pessoa> exibir() throws SQLException, IOException, ServletException{
		String sql = "from Pessoa";
/*		HttpServletRequest request = null;
		HttpServletResponse response = null;*/
		
		Session session = BancoUtil.getFactory().openSession();
		session.beginTransaction();
		
		Query query = session.createQuery(sql);

		List<Pessoa> list = query.list(); 
		session.close();
		return list;
	}

	public List<Pessoa> src(int id){
		String sql = "from Pessoa where id = "+id;

		Session session = BancoUtil.getFactory().openSession();
		session.beginTransaction();
		Query query = session.createQuery(sql);
		List<Pessoa> list = query.list();

		session.close();
		return list;
	}
	
}
